﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Infrastructure;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal;
using OpenAuth.App;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers
{
    /// <summary>
    /// 手机端巡检相关API接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MobileInspectController : ControllerBase
    {
        private readonly MobileInspectApp _app;

        /// <summary>
        /// 构造函数
        /// </summary>
        public MobileInspectController(MobileInspectApp app)
        {
            _app = app;
        }

        /// <summary>
        /// 获取当前登入用户可巡检的信息
        /// </summary>
        [HttpGet]
        public Response GetInspectInfo()
        {
            var result = new Response<MobileInspectInfo>();
            try
            {
                result.Result = _app.GetData();
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 上传巡检记录数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public Response UploadInspectRecord(InspectRecord obj)
        {
            var result = new Response();
            try
            {
                _app.UploadRecord(obj);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 获取巡检记录列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpGet]
        public Response GetInspectRecordList([FromQuery]QueryInspectRecordListReq req)
        {
            var result = new Response<List<InspectRecord>>();
            try
            {
                result.Result = _app.GetInspectRecords(req);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 获取计划列表
        /// </summary>
        [HttpGet]
        public Response GetAllPlans()
        {
            var result = new Response<List<InspectPlan>>();
            try
            {
                result.Result = _app.GetAllPlans();

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 获取设备列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response GetAllEquipList()
        {
            var result = new Response<List<EquipmentInventory>>();
            try
            {
                result.Result = _app.GetEquipmentInventories();

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 获取当前用户部门权限列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response GetUserOrgList()
        {
            var result = new Response<List<Org>>();
            try
            {
                result.Result = _app.GetOrgList();

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 获取隐患列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpGet]
        public Response GetDefectList([FromQuery]QueryDefectInventoryListReq req)
        {
            var result = new Response<List<DefectInventory>>();
            try
            {
                result.Result = _app.GetDefects(req);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
    }
}